package com.tuanzi.loan.business.repository.family.impl;

import static com.tuanzi.loan.generated.jooq.loan.Tables.MODULE_CONTENT_ITEM;
import static com.tuanzi.loan.generated.jooq.loan.tables.ActivityModel.ACTIVITY_MODEL;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanContentActivity.LOAN_CONTENT_ACTIVITY;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanContentActivityVars.LOAN_CONTENT_ACTIVITY_VARS;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.jooq.Record;
import org.jooq.Table;
import org.jooq.impl.TableImpl;
import org.springframework.stereotype.Repository;

import com.tuanzi.loan.business.enums.ModuleContentItemType;
import com.tuanzi.loan.business.repository.family.custom.ContentActivityRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.view.product.LoanProductModelView;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Repository
public class ContentActivityRepositoryImpl extends JOOQBaseRepositoryPlus implements ContentActivityRepositoryCustom {

	@Override
	protected TableImpl getTable() {
		return LOAN_CONTENT_ACTIVITY;
	}

	@Override
	public List<LoanProductModelView> loadActivityModelView(Long modelId, Long activityId) {

		Table model = dslContext.select(ACTIVITY_MODEL.ID, MODULE_CONTENT_ITEM.NAME, MODULE_CONTENT_ITEM.CONTENT, MODULE_CONTENT_ITEM.SORT, MODULE_CONTENT_ITEM.TYPE)
				.from(ACTIVITY_MODEL).leftJoin(MODULE_CONTENT_ITEM).on(ACTIVITY_MODEL.ID.eq(MODULE_CONTENT_ITEM.MODELID)).where(MODULE_CONTENT_ITEM.MODELID.isNotNull())
				.and(ACTIVITY_MODEL.ID.eq(modelId)).asTable("model");

		Table activity = dslContext.select(LOAN_CONTENT_ACTIVITY.MID, LOAN_CONTENT_ACTIVITY.ID, LOAN_CONTENT_ACTIVITY_VARS.CONTENT, LOAN_CONTENT_ACTIVITY_VARS.VAL)
				.from(LOAN_CONTENT_ACTIVITY).leftJoin(LOAN_CONTENT_ACTIVITY_VARS).on(LOAN_CONTENT_ACTIVITY.ID.eq(LOAN_CONTENT_ACTIVITY_VARS.AID))
				.where(LOAN_CONTENT_ACTIVITY_VARS.AID.eq(activityId.intValue())).asTable("activity");

		return dslContext
				.select(model.field("name").as("name"), model.field("content").as("content"), model.field("type").as("type"), model.field("sort").as("sort"),
						activity.field("val").as("val"))
				.from(model).leftJoin(activity).on(model.field("id").eq(activity.field("mid"))).and(model.field("content").eq(activity.field("content"))).where("1 = 1")
				.orderBy(model.field("sort")).fetch(record -> buildModelView(record));
	}

	private LoanProductModelView buildModelView(Record record) {
		Integer type = record.get("type", Integer.class);
		String label = record.get("name", String.class);
		String content = record.get("content", String.class);
		String val = record.get("val", String.class);

		LoanProductModelView view = new LoanProductModelView();
		view.setLabel(label);
		view.setContent(content);
		view.setType(type);

		ModuleContentItemType itemType = ModuleContentItemType.getEnum(type);
		if (null != itemType && StringUtils.isNotBlank(val)) {
			switch (itemType) {
			case INPUT_TEXT:
				view.setText(val);
				break;
			case IMAGE:
				view.setImgUrl(val);
				break;
			case IMAGE_TEXT:
				List<String> list = LoanProductModelView.getVal(val);
				if (null != list) {
					view.setImgText(list.get(0));
					view.setImgTextUrl(list.get(1));
				}
				break;
			case DATE:
				DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				try {
					view.setDate(format.parse(val));
				} catch (ParseException e) {
					log.info(e.getMessage(), e);
				}
				break;
			case RADIO_BUTTON:
				view.setIsTrue(Integer.parseInt(val));
				break;
			case LIST:
				List<String> list1 = LoanProductModelView.getVal(val);
				if (null != list1) {
					view.setOneListObjects(list1.stream().map(str -> {
						LoanProductModelView.OneListObject obj = new LoanProductModelView.OneListObject();
						obj.setText(str);
						return obj;
					}).collect(Collectors.toList()));
				}
				break;
			default:
				throw new RuntimeException("没有匹配的类型:" + type);
			}
		}
		return view;
	}
}
